/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.brain.conf.convert;

import com.brain.conf.dto.CalendarioFacturacionDto;
import com.brain.conf.manager.CalendarioFacturacionManager;
import com.brain.core.configuracion.WebServletContextListener;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import org.apache.log4j.Logger;

/**
 *
 * @author Indra
 */
@FacesConverter("calendarioFacturacionConvert")
public class CalendarioFacturacionConvert implements Converter {

    private static Logger logger = Logger.getLogger(CalendarioFacturacionConvert.class);

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        Long id = Long.valueOf(value);
        if (value.trim().equals("")) {
            return null;
        } else {
            try {
                if (id.equals(-1L)) {
                    return new CalendarioFacturacionDto(-1L);
                } else {
                    CalendarioFacturacionManager manager = (CalendarioFacturacionManager) WebServletContextListener.getApplicationContext().getBean("calendarioFacturacionManager");
                    return manager.obtenerPorId(id);
                }
            } catch (Exception exception) {
                logger.error(exception);
                return null;
            }
        }

    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null || value.equals("")) {
            return "";
        } else {
            return String.valueOf(((CalendarioFacturacionDto) value).getId());
        }
    }
}
